using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utils;

public class ShotGun : Weapon
{
    public GameObject bulletPrefab;
    public float bulletSpeed;
    public Transform pos;
    public float cd = 1;
    public float timing;

    // Start is called before the first frame update
    void Start()
    {
        effectAudio.clip = GameController.Instance().audioCtr.GetMusic("Gun/gun_01");
        pos = UITool.GetComponent<Transform>("Pos", gameObject);
        bulletPrefab.SetActive(false);
        self = PrefabRoot.ShotGun;
    }

    // Update is called once per frame
    void Update()
    {
        CheckKeysState();
#if UNITY_STANDALONE_WIN
        ChangeOrientation();
#endif
        EnemyWeaponLookAtPlayer();
    }

    public override void AttackButtonPress()
    {
        if (Time.timeScale != 0 && Time.time - timing >= cd)
        {
            if (tag.Equals("Player"))
            {
                GameController.Instance().panelCtr.playerPanel.SetMPValue(-mp);
            }
            timing = Time.time;
            float z = -15;
            for (int i = 0; i < 3; i++)
            {
                // GameObject bulletGo = Instantiate(bulletPrefab, pos.position,
                //     pos.rotation * Quaternion.AngleAxis(z, Vector3.forward));
                GameObject bulletGo = GameController.Instance().ObjectCtr.LongBullet();
                bulletGo.transform.position = pos.position;
                bulletGo.transform.rotation = pos.rotation * Quaternion.AngleAxis(z, Vector3.forward);
                bulletGo.SetActive(true);
                bulletGo.tag = transform.parent.tag;
                bulletGo.transform.parent = GameController.Instance().gameCore.transform;
                bulletGo.GetComponent<Bullet>().InitData(attack, bulletSpeed);
                z += 15;
            }
            effectAudio.Play();
        }
    }

    private void EnemyWeaponLookAtPlayer()
    {
        if (tag.Equals("Enemy"))
            UpdateLookAt(GameController.Instance().playerCtr.transform.position);
    }
}